package sz.com.postoffice.jxyzwgcase.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateStrUtil {
    private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public static String getFirstDayOfMonth(String dateStr) {
        Calendar calendar = getByDateStr(dateStr, dateFormat);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String firstDay = dateFormat.format(calendar.getTime());
        return firstDay + " 00:00:00";
    }

    public static String getYesterday(String dateStr) {
        Calendar calendar = getByDateStr(dateStr, dateFormat);
        calendar.set(Calendar.HOUR_OF_DAY, -24);
        String yesterdayDate = dateFormat.format(calendar.getTime());
        return yesterdayDate + " 23:59:59";
    }

    public static boolean isFirstDay(String dateStr) {
        Calendar calendar = getByDateStr(dateStr, dateFormat);
        return calendar.get(Calendar.DAY_OF_MONTH) == 1;
    }

    public static boolean isLastDay(String dateStr) {
        Calendar calendar = getByDateStr(dateStr, dateFormat);
        int lastDay = calendar.getActualMaximum(Calendar.DATE);
        String formatStr = dateFormat.format(calendar.getTime());
        String thisDay = formatStr.substring(formatStr.lastIndexOf("-") + 1);
        return thisDay.equals(String.valueOf(lastDay));
    }

    public static String getLastDate(String dateStr) {
        Calendar calendar = getByDateStr(dateStr, dateFormat);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        String last = dateFormat.format(calendar.getTime());
        return last + " 23:59:59";
    }

    public static String getNextDate(String dateStr) {
        Calendar calendar = getByDateStr(dateStr, dateFormat);
        calendar.add(Calendar.DATE, 1);
        String yesterdayDate = dateFormat.format(calendar.getTime());
        return yesterdayDate + " 00:00:00";
    }

    public static Calendar getByDateStr(String dateStr, DateFormat dateFormat) {
        Date date = getDateByStr(dateStr, dateFormat);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public static Date getDateByStr(String dateStr, DateFormat dateFormat) {
        Date date = new Date();
        try {
            date = dateFormat.parse(dateStr);
        } catch (ParseException e) {
            System.out.println("日期获取失败");
        }
        return date;
    }

    /**
     * 返回日期区间集合
     */
    public static List<String> getDays(String startTime, String endTime) {
        List<String> days = new ArrayList<String>();
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 获取上一周的开始日期和结束日期
     *
     * @param date
     * @return
     */
    public static String getPrevWeekDate(Date date) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date);
        int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
        int offset1 = 1 - dayOfWeek;
        int offset2 = 7 - dayOfWeek;
        calendar1.add(Calendar.DATE, offset1 - 7);
        calendar2.add(Calendar.DATE, offset2 - 7);

        String lastBeginDate = dateFormat.format(calendar1.getTime());
        String lastEndDate = dateFormat.format(calendar2.getTime());
        return lastBeginDate + "," + lastEndDate;
    }

    /**
     * 获取传入时间的上一月的第一天和最后一天
     *
     * @param date
     * @return
     */
    public static String getPrevMonthDate(Date date) {
        //获取前月的第一天
        Calendar cal_1 = Calendar.getInstance();//获取当前日期
        cal_1.setTime(date);
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        String firstDay = dateFormat.format(cal_1.getTime());

        //获取前月的最后一天
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.set(Calendar.DAY_OF_MONTH, 0);//设置为1号,当前日期既为本月第一天
        String lastDay = dateFormat.format(cale.getTime());

        return firstDay + "," + lastDay;
    }
}
